package com.credithc.customers.dataservice.service.impl;

import com.credithc.customers.base.constants.CustomerConstants;
import com.credithc.customers.dataservice.Thread.OldDataThreadPoolExecutor;
import com.credithc.customers.dataservice.constants.SyncTypeEnum;
import com.credithc.customers.dataservice.constants.TableNameConstants;
import com.credithc.customers.dataservice.mapper.XHQB.XhqbMapper;
import com.credithc.customers.dataservice.mapper.XHQB2.Xhqb2Mapper;
import com.credithc.customers.dataservice.po.CustomerNoPo;
import com.credithc.customers.dataservice.po.SystemLendPo;
import com.credithc.customers.dataservice.po.SystemUserPo;
import com.credithc.customers.dataservice.ro.CustomerAccountInfoRo;
import com.credithc.customers.dataservice.ro.CustomerProductInfoRo;
import com.credithc.customers.dataservice.service.OldXhqbDataService;
import com.credithc.customers.dataservice.service.SyncDataService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicReference;

@Slf4j
@Service
public class OldXhqbDataServiceImpl implements OldXhqbDataService {
    @Autowired
    SyncDataService syncDataService;
    @Autowired
    private Xhqb2Mapper xhqb2Mapper;
    @Autowired
    XhqbMapper xhqbMapper;
    @Autowired
    private OldDataThreadPoolExecutor oldDataThreadPoolExecutor;

    @Override
    public void oldOrder() {

        boolean f =true;
        Long num = xhqbMapper.queryLendMaxId();
        if (num == null){
            return;
        }
        AtomicReference<Long> vernier = new AtomicReference<>( num+1001);
        ExecutorService e = oldDataThreadPoolExecutor.getCustomThreadPoolExecutor();
        while (f) {
            e.submit(() -> {
                try {
                    if (vernier.get()>0){
                        vernier.set(vernier.get()-1000);
                    }
                    List<SystemLendPo> systemLendPos = xhqbMapper.queryXhqbLend(Long.toString(vernier.get()));
                    if (systemLendPos.size() > 0) {
                        bulidXhqbLend(systemLendPos);
                    }
                } catch (Exception e1) {
                    log.error(e1.getMessage());
                }
            });
            f = OldHyrDataServiceImpl.exit(f, vernier, e);
        }

    }

    @Override
    public void oldBank() {
        boolean f =true;
        Long num = xhqbMapper.queryBankMaxId();
        if (num == null){
            return;
        }
        AtomicReference<Long> vernier = new AtomicReference<>( num+1001);
        ExecutorService e = oldDataThreadPoolExecutor.getCustomThreadPoolExecutor();
        while (f) {
            e.submit(() -> {
                try {
                    if (vernier.get()>0){
                        vernier.set(vernier.get()-1000);
                    }
                    List<SystemUserPo> systemLendPos = xhqbMapper.queryXhqbBank(Long.toString(vernier.get()));
                    if (systemLendPos.size() > 0) {
                        bulidXhqbBank(systemLendPos);
                    }
                } catch (Exception e1) {
                    log.error(e1.getMessage());
                }
            });
            f = OldHyrDataServiceImpl.exit(f, vernier, e);
        }

    }

    private void bulidXhqbBank(List<SystemUserPo> systemLendPos) {
        List<CustomerAccountInfoRo> customerAccountInfoRos = new ArrayList<>();
        for (SystemUserPo s:systemLendPos) {
            CustomerAccountInfoRo customerAccountInfoRo = new CustomerAccountInfoRo();
            customerAccountInfoRo.setSyncType(SyncTypeEnum.NEW.getValue());
            customerAccountInfoRo.setSubSystemSign("XHQB");
            customerAccountInfoRo.setSystemSign("XHQB");
            customerAccountInfoRo.setAccountCreateTime(s.getCreatTime());
            customerAccountInfoRo.setAccountNo(s.getCustomerId());
            customerAccountInfoRo.setCustomerRole(CustomerConstants.CUSTOMER_ACCOUNT_TYPE_LOAN);
           // customerAccountInfoRo.setCustomerNo(u.getCustomerNo());
            customerAccountInfoRo.setAccountType(CustomerConstants.CUSTOMER_ACCOUNT_TYPE_BANK_CARD);
            customerAccountInfoRo.setUserSourceId(s.getUserId());
            customerAccountInfoRo.setAccountStatus(StringUtils.isNotBlank(customerAccountInfoRo.getAccountNo())?CustomerConstants.STATUS_ACCOUNT_AVAILABLE:CustomerConstants.STATUS_ACCOUNT_UNAVAILABLE);

            CustomerNoPo byUserId = xhqb2Mapper.getByUserId(s.getUserId());
            if (byUserId != null){
                customerAccountInfoRo.setCustomerNo(byUserId.getCustomerNo());
            }
            if (StringUtils.isNotBlank(customerAccountInfoRo.getCustomerNo())) {
                customerAccountInfoRos.add(customerAccountInfoRo);
            }
        }
        syncDataService.syncCustomerAccount(customerAccountInfoRos);
    }

    private void bulidXhqbLend(List<SystemLendPo> dlrLendRos) {
        List<CustomerProductInfoRo> customerProductInfoRos = new ArrayList<>();
        for (SystemLendPo d:dlrLendRos){
            CustomerProductInfoRo customerProductInfoRo = new CustomerProductInfoRo();
            customerProductInfoRo.setDatabaseName(TableNameConstants.XHQB_DATABASE);
            customerProductInfoRo.setTableName(TableNameConstants.XHQB_CUSTOMER_PRODUCT_HW_ORDER);
            customerProductInfoRo.setSyncType(SyncTypeEnum.NEW.getValue());
            customerProductInfoRo.setSubSystemSign("XHQB");
            customerProductInfoRo.setSystemSign("XHQB");
            customerProductInfoRo.setProductSourceSystem(CustomerConstants.XHQB_CODE+"");
            //customerProductInfoRo.setCustomerRoleStatus(1);
            customerProductInfoRo.setProductAmount(new BigDecimal(Integer.valueOf(d.getAmount()+"")/100));
            customerProductInfoRo.setProductNo(TableNameConstants.XHQB_JYFQ_CODE);
            customerProductInfoRo.setProductStartTime(d.getCapitalEffectDate());
            customerProductInfoRo.setProductStatus(CustomerProductInfoServiceImpl.getYZTStatus("XHQB"+d.getSts()));
            customerProductInfoRo.setUserResourceId(d.getUserId());

            if (d.getPeriod_type() == 2){
                customerProductInfoRo.setProductDeadline((d.getE_period()+d.getL_period())*30+"");
            }else{
                customerProductInfoRo.setProductDeadline(d.getPeriod()*30+"");
            }

            CustomerNoPo byUserId = xhqb2Mapper.getByUserId(d.getUserId());
            if (byUserId !=null){
                customerProductInfoRo.setCustomerNo(byUserId.getCustomerNo());
                customerProductInfoRo.setPreCustomerNo(byUserId.getPreCustomerNo());
               /* if (byUserId.getCustomerNo()!=null){
                    customerProductInfoRo.setCustomerRole(getCustomerRole(byUserId.getCustomerNo(),"XHQB","XHQB"));
                }else{
                    customerProductInfoRo.setCustomerRole(getCustomerRole(byUserId.getPreCustomerNo(),"XHQB","XHQB"));
                }*/
            }
            if (StringUtils.isNotBlank(customerProductInfoRo.getCustomerNo())) {
                customerProductInfoRos.add(customerProductInfoRo);
            }
        }
        syncDataService.syncCustomerProduct(customerProductInfoRos);
    }

  /*  private String getCustomerRole(String customerNo,String systemSign, String subSystemSign){
        String roleSource = commonService.getRoleSource(systemSign, subSystemSign);
        String destRole = null;
        if(StringUtils.isNotBlank(roleSource)){
            if(!CustomerConstants.RELATION_SOURCE_LIST.contains(roleSource)){
                //表示角色是固定值
                destRole =  roleSource;
            } else if (CustomerConstants.ROLE_SOURCE_PRODUCT.equals(roleSource)){
                //TODO 表示要从产品购买记录分析出角色 destRole = ??
                destRole =  null;
            }

            if(StringUtils.isNotBlank(destRole)){
                CustomerRolePo customerRole = commonService.getCustomerRole(customerNo, destRole, systemSign, subSystemSign);
                if(customerRole == null){
                    return destRole;
                }
            }
        }
        return null;
    }*/
}
